nanopyx.core.io.zip_image_loader
1import os 2import zipfile 3 4import numpy as np 5import skimage.io 6from skimage import transform 7 8 9class ZipTiffIterator: 10 _shape = None 11 _dtype = None 12 _im0 = None 13 14 def __init__(self, zip_file_path: str): 15 """ 16 Iterator to sequentially open tiff files contained in a zip file 17 :param zip_file_path: path to the zip file 18 :type zip_file_path: str 19 """ 20 self.zip_file_path = zip_file_path 21 self.zip_file = zipfile.ZipFile(zip_file_path) 22 self.tiff_file_names = [ 23 name 24 for name in self.zip_file.namelist() 25 if name.endswith(".tif") and not name.startswith("_") 26 ] 27 self.tiff_file_names = sorted(self.tiff_file_names) 28 # print(self.tiff_file_names) 29 30 def __getitem__(self, index: int) -> np.ndarray: 31 if index >= len(self.tiff_file_names): 32 raise IndexError 33 else: 34 with self.zip_file.open(self.tiff_file_names[index]) as tiff_file: 35 image = skimage.io.imread(tiff_file) 36 return np.array(image) 37 38 def __len__(self) -> int: 39 return len(self.tiff_file_names) 40 41 def __iter__(self): 42 for index in range(len(self.tiff_file_names)): 43 yield self[index] 44 45 def _get_im0(self): 46 if self._im0 is None: 47 self._im0 = self[0] 48 self._shape = ( 49 len(self.tiff_file_names), 50 self._im0.shape[0], 51 self._im0.shape[1], 52 ) 53 self._dtype = self._im0.dtype 54 return self._im0 55 56 def get_shape(self) -> list: 57 """ 58 Returns the shape of the image stack 59 :return: shape of the image stack 60 :rtype: list 61 """ 62 self._get_im0() 63 return self._shape 64 65 def get_dtype(self) -> str: 66 """ 67 Returns the data type of the image stack 68 :return: data type of the image stack 69 :rtype: str 70 """ 71 self._get_im0() 72 return self._dtype 73 74 def get_thumb(self, save_path=None): 75 """ 76 Saves a thumbnail (first frame) of the image stack 77 :param save_path: path to save the thumbnail 78 :type save_path: str 79 """ 80 thumb = transform.resize(self._get_im0(), (64, 64)) 81 _max = thumb.max() 82 _min = thumb.min() 83 thumb = (thumb.astype("float32") - _min / (_max - _min)) * 255 84 if save_path != None: 85 # Save the thumbnail as a JPEG file 86 skimage.io.imsave(os.path.join(save_path, "thumbnail.jpg"), thumb) 87 88 def close(self): 89 """ 90 Closes the zip file 91 """ 92 self.zip_file.close()
class
ZipTiffIterator:
10class ZipTiffIterator: 11 _shape = None 12 _dtype = None 13 _im0 = None 14 15 def __init__(self, zip_file_path: str): 16 """ 17 Iterator to sequentially open tiff files contained in a zip file 18 :param zip_file_path: path to the zip file 19 :type zip_file_path: str 20 """ 21 self.zip_file_path = zip_file_path 22 self.zip_file = zipfile.ZipFile(zip_file_path) 23 self.tiff_file_names = [ 24 name 25 for name in self.zip_file.namelist() 26 if name.endswith(".tif") and not name.startswith("_") 27 ] 28 self.tiff_file_names = sorted(self.tiff_file_names) 29 # print(self.tiff_file_names) 30 31 def __getitem__(self, index: int) -> np.ndarray: 32 if index >= len(self.tiff_file_names): 33 raise IndexError 34 else: 35 with self.zip_file.open(self.tiff_file_names[index]) as tiff_file: 36 image = skimage.io.imread(tiff_file) 37 return np.array(image) 38 39 def __len__(self) -> int: 40 return len(self.tiff_file_names) 41 42 def __iter__(self): 43 for index in range(len(self.tiff_file_names)): 44 yield self[index] 45 46 def _get_im0(self): 47 if self._im0 is None: 48 self._im0 = self[0] 49 self._shape = ( 50 len(self.tiff_file_names), 51 self._im0.shape[0], 52 self._im0.shape[1], 53 ) 54 self._dtype = self._im0.dtype 55 return self._im0 56 57 def get_shape(self) -> list: 58 """ 59 Returns the shape of the image stack 60 :return: shape of the image stack 61 :rtype: list 62 """ 63 self._get_im0() 64 return self._shape 65 66 def get_dtype(self) -> str: 67 """ 68 Returns the data type of the image stack 69 :return: data type of the image stack 70 :rtype: str 71 """ 72 self._get_im0() 73 return self._dtype 74 75 def get_thumb(self, save_path=None): 76 """ 77 Saves a thumbnail (first frame) of the image stack 78 :param save_path: path to save the thumbnail 79 :type save_path: str 80 """ 81 thumb = transform.resize(self._get_im0(), (64, 64)) 82 _max = thumb.max() 83 _min = thumb.min() 84 thumb = (thumb.astype("float32") - _min / (_max - _min)) * 255 85 if save_path != None: 86 # Save the thumbnail as a JPEG file 87 skimage.io.imsave(os.path.join(save_path, "thumbnail.jpg"), thumb) 88 89 def close(self): 90 """ 91 Closes the zip file 92 """ 93 self.zip_file.close()
ZipTiffIterator(zip_file_path: str)
15 def __init__(self, zip_file_path: str): 16 """ 17 Iterator to sequentially open tiff files contained in a zip file 18 :param zip_file_path: path to the zip file 19 :type zip_file_path: str 20 """ 21 self.zip_file_path = zip_file_path 22 self.zip_file = zipfile.ZipFile(zip_file_path) 23 self.tiff_file_names = [ 24 name 25 for name in self.zip_file.namelist() 26 if name.endswith(".tif") and not name.startswith("_") 27 ] 28 self.tiff_file_names = sorted(self.tiff_file_names) 29 # print(self.tiff_file_names)
Iterator to sequentially open tiff files contained in a zip file
Parameters
- zip_file_path: path to the zip file
def
get_shape(self) -> list:
57 def get_shape(self) -> list: 58 """ 59 Returns the shape of the image stack 60 :return: shape of the image stack 61 :rtype: list 62 """ 63 self._get_im0() 64 return self._shape
Returns the shape of the image stack
Returns
shape of the image stack
def
get_dtype(self) -> str:
66 def get_dtype(self) -> str: 67 """ 68 Returns the data type of the image stack 69 :return: data type of the image stack 70 :rtype: str 71 """ 72 self._get_im0() 73 return self._dtype
Returns the data type of the image stack
Returns
data type of the image stack
def
get_thumb(self, save_path=None):
75 def get_thumb(self, save_path=None): 76 """ 77 Saves a thumbnail (first frame) of the image stack 78 :param save_path: path to save the thumbnail 79 :type save_path: str 80 """ 81 thumb = transform.resize(self._get_im0(), (64, 64)) 82 _max = thumb.max() 83 _min = thumb.min() 84 thumb = (thumb.astype("float32") - _min / (_max - _min)) * 255 85 if save_path != None: 86 # Save the thumbnail as a JPEG file 87 skimage.io.imsave(os.path.join(save_path, "thumbnail.jpg"), thumb)
Saves a thumbnail (first frame) of the image stack
Parameters
- save_path: path to save the thumbnail